import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * Created by brochexu on 6/18/15.
 */
public class Main {
    public static final void main(String[] args) throws DocumentException, IOException {
        SAXReader reader = new SAXReader();
        Document document = reader.read(new File("recipes.xml"));
        Element root = document.getRootElement();
        List<Element> elements = root.elements("recipe");
        System.out.println("data initialized");
        Scanner scanner = new Scanner(System.in);
        boolean exitFlag = false;
        while (!exitFlag) {
            System.out.println("item name(q for exit): ");
            String input = scanner.next();
            if (input.trim().equals("q")) {
                exitFlag = true;
            } else {
                List<Element> list = new ArrayList<Element>();
                for (Element element : elements) {
                    Attribute attribute = element.attribute("name");
                    if (attribute.getText().contains(input.trim())) {
                        list.add(element);
                    }
                }
                if (list.size() <= 0) {
                    System.out.println("no such item.");
                } else if (list.size() == 1) {
                    Element element = list.get(0);
                    printElement(element);
                } else {
                    boolean cancelFlag = false;
                    while (!cancelFlag) {
                        for (int i = 0; i < list.size(); i++) {
                            Attribute attribute = list.get(i).attribute("name");
                            System.out.println(i + " " + attribute.getText());
                        }
                        System.out.println("item index(c for cancel): ");
                        input = scanner.next();
                        if (input.trim().equals("c")) {
                            cancelFlag = true;
                        } else {
                            int inputIndex = Integer.parseInt(input);
                            printElement(list.get(inputIndex));
                            System.out.print("back to list? (y/n): ");
                            input = scanner.next();
                            if (input.trim().equals("n")) {
                                cancelFlag = true;
                            }
                        }
                    }
                }
            }
        }
    }

    private static void printElement(Element element) {
        System.out.println("==============================");
        System.out.println("Craft name: " + element.attribute("name").getText());
        Attribute atting = element.attribute("ingredients");
        Attribute attneed = element.attribute("craft_need");
        if (attneed != null) {
            System.out.println("Craft needed: " + attneed.getText());
        }
        String[] ingarr = atting.getText().split(",");
        String[][] ingresult = new String[3][3];
        int maxLen = 0;
        for (String ing : ingarr) {
            String[] t = ing.split("-");
            if (t[0].length() > maxLen) {
                maxLen = t[0].length();
            }
            int index = Integer.parseInt(t[1]) - 1;
            int r = index / 3;
            int c = index % 3;
            ingresult[r][c] = t[0];
        }
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                String str = ingresult[i][j];
                System.out.print(getFixedLenString(str, maxLen, ' ') + "\t");
            }
            System.out.println();
        }
        System.out.println("==============================");
    }

    public static String getFixedLenString(String str, int len, char c) {
        if (str == null || str.length() == 0) {
            str = "";
        }
        if (str.length() == len) {
            return str;
        }
        if (str.length() > len) {
            return str.substring(0, len);
        }
        StringBuilder sb = new StringBuilder(str);
        while (sb.length() < len) {
            sb.append(c);
        }
        return sb.toString();
    }
}
